﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeGraphicalControl;
using FeatureModelCore;
using Collections.TreeCollections;
using System.Windows.Controls;
using System.Windows;
using FeatureModelTool.FeatureModelView.MenuItems;
using FeatureModelTool.Windows;
using FeatureModelCore.FMTypes;
using System.Windows.Media;

namespace FeatureModelTool.FeatureModelView.TreeViewItems
{
    public class TreeItem_GroupRelationship : TreeViewItemControl
    {
        public FeatureTreeGroupRelationship FeatureInstance { get; protected set; }

        private readonly static string _ICON_SOURCE = "/FeatureModelTool;component/Images/g.png";
        protected Brush Backgroud
        {
            get
            {
                if (FeatureInstance.ExistError)
                    return FeatureTreeView.ERROR_BACKGROUND;
                if (FeatureInstance.ExistWarning)
                    return FeatureTreeView.WARNING_BACKGROUND;
                return FeatureTreeView.COMMON_BACKGROUND;
            }
        }

        public TreeItem_GroupRelationship(FeatureTreeGroupRelationship gRelationship)
        {
            FeatureInstance = gRelationship;
            string header = string.Format("{0} Feature Group Relationship", gRelationship.Type.ToString());
            this.Header = MainWindow.GenerateTreeItemHeader(header, _ICON_SOURCE
                , Backgroud);
            //this.BorderBrush = Brushes.Black;
            //this.BorderThickness = new Thickness(1, 1, 1, 1);
        }

        /// <summary>
        /// Tree Item Construct
        /// </summary>
        /// <param name="treeObj"></param>
        /// <returns></returns>
        public static TreeViewItemControl ConstructTreeItem(TreeObject treeObj)
        {
            FeatureTreeGroupRelationship projFile = treeObj as FeatureTreeGroupRelationship;
            if (projFile == null)
            {
                return null;
            }
            return new TreeItem_GroupRelationship(projFile);
        }

        #region Context Menu
        /// <summary>
        /// Context Generator
        /// </summary>
        /// <param name="treeRoot"></param>
        /// <returns></returns>
        public static ContextMenu GenerateContext(TreeViewItemControl treeItem, TreeObject treeObj)
        {
            FeatureTreeGroupRelationship gRelationship = treeObj as FeatureTreeGroupRelationship;
            TreeItem_GroupRelationship treeItem_GR = treeItem as TreeItem_GroupRelationship;
            if (gRelationship == null
                || treeItem_GR == null)
            {
                return null;
            }
            // new Relationshp
            MenuItem_GroupRelationship newR = new MenuItem_GroupRelationship(treeItem_GR, gRelationship)
            {
                Name = gRelationship.ID.ToString(),
                Header = "New Feature Node"
            };
            newR.Click += new RoutedEventHandler(NewFeatureNode_EventHandler);

            // edit
            MenuItem_GroupRelationship edit = new MenuItem_GroupRelationship(treeItem_GR, gRelationship)
            {
                Name = gRelationship.ID.ToString(),
                Header = "Edit"
            };
            edit.Click += new RoutedEventHandler(Edit_EvenetHandler);

            // delete Group Relationship
            MenuItem_GroupRelationship delete = new MenuItem_GroupRelationship(treeItem_GR, gRelationship)
            {
                Name = gRelationship.ID.ToString(),
                Header = "Delete"
            };
            delete.Click += new RoutedEventHandler(TreeItem_Root.Delete_EventHanlder);

            ContextMenu contextMenu = new ContextMenu();
            contextMenu.Items.Add(newR);
            contextMenu.Items.Add(edit);
            contextMenu.Items.Add(delete);
            return contextMenu;
        }
        /// <summary>
        /// New Feature Tree Node
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void NewFeatureNode_EventHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_GroupRelationship gRelationship = sender as MenuItem_GroupRelationship;
            if (gRelationship == null)
            {
                return;
            }

            NewRelationshipDialog dia = new NewRelationshipDialog(string.Empty);
            dia.ShowDialog();

            if (dia.Dirty == false)
            {
                return;
            }


            FeatureModelTree tree = (FeatureModelTree)gRelationship.FeatureInstance.TreeOwner;
            tree.CreateFeatureNode(gRelationship.FeatureInstance, dia.FeatureName);
        }
        /// <summary>
        /// Edit Type of cross contraints
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void Edit_EvenetHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_GroupRelationship gR = sender as MenuItem_GroupRelationship;
            if (gR == null)
            {
                return;
            }
            Array types = Enum.GetValues(typeof(FMTreeGroupRelationshipType));
            EditTypeDialog dia = new EditTypeDialog(types, gR.FeatureInstance.Type);
            dia.ShowDialog();

            if (dia.Dirty == true)
            {
                gR.FeatureInstance.Type = (FMTreeGroupRelationshipType)dia.RType;
            }
        }
        #endregion
    }
}
